package com.sctvcloud.zhaohua_sd.ui.adapter.holder;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.TextSwitcher;
import android.widget.ViewSwitcher;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.FSubscribe;
import com.sctvcloud.zhaohua_sd.beans.LiveHomeAdvance;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by rh008 on 2017/7/13.
 */

public class LiveHomeAdvanceHolder extends BaseAbsHolder<LiveHomeAdvance> implements IAndroidLittleLife, View.OnClickListener {

    @BindView(R.id.item_live_ad_title)
    protected TextSwitcher title;
    @BindView(R.id.item_live_ad_num)
    protected CustomFontTextView num;

    private boolean hasInManager;

    private int lifeMode;

    private boolean hasCalledResume;//是否已经执行过resume

    private int currentPos;
    private List<FSubscribe> datas;
    private FSubscribe currentAd;

    private Handler handler;

    private final long delay = 3000;

    private CustomFontTextView tvtemp;
    private int types = 1;

    public LiveHomeAdvanceHolder(final Context context, View itemView) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg == null) {
                    return;
                }
                if (msg.what == 1) {
                    handler.removeMessages(1);
                    next();
                    handler.sendEmptyMessageDelayed(1, delay);
                } else if (msg.what == 2) {
                    handler.removeMessages(1);
                    handler.removeMessages(2);
                }
            }
        };
        title.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                CustomFontTextView tv = (CustomFontTextView) LayoutInflater.from(context).inflate(R.layout.view_advance_title, null);
               // tv.setSelected(true);
                JLog.e("new text " + tv);
                tvtemp = tv;
                return tv;
            }
        });

        title.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.switcher_anim_in));
        title.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.switcher_anim_out));
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(LiveHomeAdvance liveHomeAdvance) {
        if (ListUtils.isListValued(liveHomeAdvance.getList())) {
            itemView.setVisibility(View.VISIBLE);
        } else {
            itemView.setVisibility(View.GONE);
            return;
        }
        List<FSubscribe> list = liveHomeAdvance.getList();
        num.setText(list.size() + "场");
        if (list == datas) {
            return;
        }
        datas = list;
        handler.sendEmptyMessage(1);
        if (datas.size() == 1) {
            handler.sendEmptyMessage(2);
        }

    }


    @Override
    public int onIResume() {
        hasCalledResume = true;
        if (lifeMode > 0) {
            if (datas != null && datas.size() > 1) {
                handler.sendEmptyMessage(1);
            }
        }
        return 0;
    }

    @Override
    public int onIPause() {
        handler.sendEmptyMessage(2);
        return 0;
    }

    @Override
    public int onIDestory() {
        handler.sendEmptyMessage(2);
        handler = null;
        context = null;
        return 0;
    }

    @Override
    public void setHasInManager(boolean inManager) {
        this.hasInManager = inManager;
    }

    @Override
    public boolean hasInManager() {
        return hasInManager;
    }

    @Override
    public void setLifeMode(int mode) {
        lifeMode = mode;
    }

    private void next() {
//        JLog.e("will next and current pos is "+currentPos+"  timer:"+timer+" datas:"+datas);
//        JLog.e(tvtemp.getTextScaleX()+"  "+tvtemp.getOffsetForPosition(0,0)+"  "+tvtemp.getText().toString());
        currentPos++;
        if (currentPos >= datas.size()) {
            currentPos = 0;
        }
        currentAd = datas.get(currentPos);
//            title.setText(currentBanner.getTitle());
        title.setText(currentAd.getLiveTitle());
        title.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        if (internalClick != null) {
            internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
        }
    }
}
